import { defineStore } from 'pinia'
import {
  GetDirectoryList,
  GetItemFileInfo,
  SaveItem,
  DeleteItem,
  SaveItemFile,
  DeleteItemFile,
  RestoreItemFile,
} from '@/api'
import { FileItem, ItemFileInfo, ResponseDto, ItemInfo } from '@/api/types'

export const useItemsStore = defineStore({
  // id: 必须的，在所有 Store 中唯一
  id: 'itemsStore',
  // state: 返回对象的函数
  state: () => ({
    directoryList: [] as FileItem[],
    nowFileItem: {} as FileItem,
  }),
  getters: {
    DIRECTORY_LIST(): FileItem[] {
      return this.directoryList
    },
  },
  // 可以同步 也可以异步
  actions: {
    init() {
      GetDirectoryList()
        .then((result) => {
          this.directoryList = result.data.reverse()
        })
        .catch((err) => {})
    },
    saveItemFile(uuid: string, url: string, title: string): Promise<ResponseDto<string>> {
      return new Promise((reslove, reject) => {
        SaveItemFile({
          uuId: uuid,
          url: url,
          title: title,
        })
          .then((result) => {
            if (result.code === 200) {
              this.init()
              reslove(result)
            } else {
              reject(result)
            }
          })
          .catch((err) => {
            reject(err)
          })
      })
    },
    restoreItemFile(uuid: string) {
      return new Promise((reslove, reject) => {
        RestoreItemFile(uuid)
          .then((result) => {
            if (result.code === 200) {
              this.init()
              reslove(result)
            } else {
              reject(result)
            }
          })
          .catch((err) => {
            reject(err)
          })
      })
    },
    deleteItemFile(uuid: string) {
      return new Promise((reslove, reject) => {
        DeleteItemFile(uuid)
          .then((result) => {
            if (result.code === 200) {
              this.init()
              reslove(result)
            } else {
              reject(result)
            }
          })
          .catch((err) => {
            reject(err)
          })
      })
    },
    getItemFileInfo(uuid: string): Promise<ResponseDto<ItemFileInfo>> {
      return new Promise((reslove, reject) => {
        GetItemFileInfo(uuid)
          .then((result) => {
            if (result.code === 200) {
              this.nowFileItem = result.data
              reslove(result)
            } else {
              reject(result)
            }
          })
          .catch((err) => {
            reject(err)
          })
      })
    },
    getItemInfo(id: string): ItemInfo {
      return this.nowFileItem.items.find((t) => t.id === id)
    },
    saveItemInfo(id: string, url: string, label: string): Promise<ResponseDto<boolean>> {
      return new Promise((reslove, reject) => {
        SaveItem({
          uuId: this.nowFileItem.uuid,
          id: id,
          url: url,
          label: label,
        })
          .then((result) => {
            if (result.code === 200) {
              reslove(result)
            } else {
              reject(result)
            }
          })
          .catch((err) => {
            reject(err)
          })
      })
    },
    deleteItem(id: string) {
      return new Promise((reslove, reject) => {
        DeleteItem({
          uuId: this.nowFileItem.uuid,
          id: id,
        })
          .then((result) => {
            if (result.code === 200) {
              reslove(result)
            } else {
              reject(result)
            }
          })
          .catch((err) => {
            reject(err)
          })
      })
    },
  },
  // 进行持久化存储
  persist: {
    // 本地存储的名称
    key: 'directoryLisLt',
    //保存的位置
    storage: window.localStorage, //localstorage
  },
})
